package com.ruoyi.system.utils;

import com.ruoyi.system.service.ISysConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 日志配置
 *
 * @author ruoyi
 */
@Component
public class LogUtil {
    @Autowired
    private ISysConfigService configService;
    // 日志开关配置键
    private static final String LOG_SWITCH_KEY = "log.enabled";

    private static final Logger log = LoggerFactory.getLogger(LogUtil.class);

    public void info(String name, Object msg) {
        if (isCheck(name, msg)) return;
        log.info("[{}] {}", name, msg);
    }

    public void debug(String name, Object msg) {
        if (isCheck(name, msg)) return;
        log.debug("[{}] {}", name, msg);
    }

    public void warn(String name, Object msg) {
        if (isCheck(name, msg)) return;
        log.warn("[{}] {}", name, msg);
    }

    public void error(String name, Object msg) {
        if (isCheck(name, msg)) return;
        log.error("[{}] {}", name, msg);
    }

    private boolean isCheck(String name, Object msg) {
        if (msg == null) {
            return true;
        }
        return !configService.selectConfigByKey(LOG_SWITCH_KEY).contains(name);
    }

}
